package cn.edu.fudan.bdke.ncov.data.epidemic;

import cn.edu.fudan.bdke.ncov.data.epidemic.model.City;
import cn.edu.fudan.bdke.ncov.data.epidemic.model.Epidemic;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 湖北省每日疫情情况采集
 *
 * @author xym
 * @since 2020/2/13
 */
public class HuBeiEpidemicSituation extends EpidemicSituation {
    private HuBeiEpidemicSituation() {
        super();
        List<String> cities = getCities("src/main/resources/cityInfo/hubei.txt");
        for (String city : cities) {
            this.cityEpidemicMap.put(city, new Epidemic());
        }
    }

    private static HuBeiEpidemicSituation instance;

    public static HuBeiEpidemicSituation getInstance() {
        if (instance == null) {
            instance = new HuBeiEpidemicSituation();
        }
        return instance;
    }

    @Override
    public void collectData(String document) {
        Pattern pattern = Pattern.compile("[0-9]{4}年[0-9]{1,2}月[0-9]{1,2}日");
        Matcher matcher = pattern.matcher(document);
        if (matcher.find()) {
            this.date = matcher.group();
        }

        String[] sentences = document.split("。");
        for (String sentence : sentences) {
            // 统计新增确诊病例情况
            if (sentence.contains("新增新冠肺炎病例")) {
                int index = sentence.indexOf("新增新冠肺炎病例");
                String subStr = sentence.substring(index);

                //全省新增病例
                Pattern p = Pattern.compile("[0-9]+");
                Matcher m = p.matcher(subStr);
                if (m.find()) {
                    totalEpidemic.setNewlyIncreasedConfirm(Integer.parseInt(m.group()));
                }

                //地级行政区新增病例
                int indexStart = subStr.indexOf("其中");
                if (indexStart >= 0) {
                    String str = subStr.substring(indexStart);
                    Set<String> cities = this.cityEpidemicMap.keySet();
                    for (String city : cities) {
                        String regEx = city + "?[0-9]+";
                        Pattern p1 = Pattern.compile(regEx);
                        Matcher m1 = p1.matcher(str);
                        if (m1.find()) {
                            // number示例：武汉市12345例
                            String number = m1.group(0);
                            Pattern p2 = Pattern.compile("[0-9]+");
                            Matcher m2 = p2.matcher(number);
                            if (m2.find()) {
                                Epidemic epidemic = cityEpidemicMap.get(city);
                                epidemic.setNewlyIncreasedConfirm(Integer.parseInt(m2.group()));
                            }
                        }
                    }
                }
            }

            //统计新增死亡病例情况
            if (sentence.contains("新增病亡")) {
                int index = sentence.indexOf("新增病亡");
                String subStr = sentence.substring(index);

                //全省病例
                Pattern p = Pattern.compile("[0-9]+");
                Matcher m = p.matcher(subStr);
                if (m.find()) {
                    totalEpidemic.setNewlyIncreasedDeath(Integer.parseInt(m.group()));
                }

                //地级行政区病例
                int indexStart = subStr.indexOf("其中");
                if (indexStart >= 0) {
                    String str = subStr.substring(indexStart);
                    Set<String> cities = this.cityEpidemicMap.keySet();
                    for (String city : cities) {
                        String regEx = city + "?[0-9]+";
                        Pattern p1 = Pattern.compile(regEx);
                        Matcher m1 = p1.matcher(str);
                        if (m1.find()) {
                            // number示例：武汉市12345例
                            String number = m1.group();
                            Pattern p2 = Pattern.compile("[0-9]+");
                            Matcher m2 = p2.matcher(number);
                            if (m2.find()) {
                                Epidemic epidemic = cityEpidemicMap.get(city);
                                epidemic.setNewlyIncreasedDeath(Integer.parseInt(m2.group()));
                            }
                        }
                    }
                }
            }

            if (sentence.contains("新增死亡")) {
                int index = sentence.indexOf("新增死亡");
                String subStr = sentence.substring(index);

                //全省病例
                Pattern p = Pattern.compile("[0-9]+");
                Matcher m = p.matcher(subStr);
                if (m.find()) {
                    totalEpidemic.setNewlyIncreasedDeath(Integer.parseInt(m.group()));
                }

                //地级行政区病例
                int indexStart = subStr.indexOf("其中");
                if (indexStart >= 0) {
                    String str = subStr.substring(indexStart);
                    Set<String> cities = this.cityEpidemicMap.keySet();
                    for (String city : cities) {
                        String regEx = city + "?[0-9]+";
                        Pattern p1 = Pattern.compile(regEx);
                        Matcher m1 = p1.matcher(str);
                        if (m1.find()) {
                            // number示例：武汉市12345例
                            String number = m1.group();
                            Pattern p2 = Pattern.compile("[0-9]+");
                            Matcher m2 = p2.matcher(number);
                            if (m2.find()) {
                                Epidemic epidemic = cityEpidemicMap.get(city);
                                epidemic.setNewlyIncreasedDeath(Integer.parseInt(m2.group()));
                            }
                        }
                    }
                }
            }

            //统计新增治愈病例情况
            if (sentence.contains("新增出院")) {
                int index = sentence.indexOf("新增出院");
                String subStr = sentence.substring(index);

                //全省病例
                Pattern p = Pattern.compile("[0-9]+");
                Matcher m = p.matcher(subStr);
                if (m.find()) {
                    totalEpidemic.setNewlyIncreasedCuredCase(Integer.parseInt(m.group()));
                }

                //地级行政区病例
                int indexStart = subStr.indexOf("其中");
                if (indexStart >= 0) {
                    String str = subStr.substring(indexStart);
                    Set<String> cities = this.cityEpidemicMap.keySet();
                    for (String city : cities) {
                        String regEx = city + "?[0-9]+";
                        Pattern p1 = Pattern.compile(regEx);
                        Matcher m1 = p1.matcher(str);
                        if (m1.find()) {
                            // number示例：武汉市12345例
                            String number = m1.group();
                            Pattern p2 = Pattern.compile("[0-9]+");
                            Matcher m2 = p2.matcher(number);
                            if (m2.find()) {
                                Epidemic epidemic = cityEpidemicMap.get(city);
                                epidemic.setNewlyIncreasedCuredCase(Integer.parseInt(m2.group()));
                            }
                        }
                    }
                }
            }

            //统计累计确诊病例情况
            if (sentence.contains("累计报告新冠肺炎")) {
                int index = sentence.indexOf("累计报告新冠肺炎");
                String subStr = sentence.substring(index);

                //全省病例
                Pattern p = Pattern.compile("[0-9]+");
                Matcher m = p.matcher(subStr);
                if (m.find()) {
                    totalEpidemic.setAccumulatedConfirm(Integer.parseInt(m.group()));
                }

                //地级行政区病例
                int indexStart = subStr.indexOf("其中");
                if (indexStart >= 0) {
                    String str = subStr.substring(indexStart);
                    Set<String> cities = this.cityEpidemicMap.keySet();
                    for (String city : cities) {
                        String regEx = city + "?[0-9]+";
                        Pattern p1 = Pattern.compile(regEx);
                        Matcher m1 = p1.matcher(str);
                        if (m1.find()) {
                            // number示例：武汉市12345例
                            String number = m1.group();
                            Pattern p2 = Pattern.compile("[0-9]+");
                            Matcher m2 = p2.matcher(number);
                            if (m2.find()) {
                                Epidemic epidemic = cityEpidemicMap.get(city);
                                epidemic.setAccumulatedConfirm(Integer.parseInt(m2.group()));
                            }
                        }
                    }
                }
            }

            //统计累计治愈
            if (sentence.contains("累计治愈出院")) {
                int index = sentence.indexOf("累计治愈出院");
                String subStr = sentence.substring(index);

                //全省病例
                Pattern p = Pattern.compile("[0-9]+");
                Matcher m = p.matcher(subStr);
                if (m.find()) {
                    totalEpidemic.setAccumulatedCuredCase(Integer.parseInt(m.group()));
                }

                //地级行政区病例
                int indexStart = subStr.indexOf("其中");
                if (indexStart >= 0) {
                    String str = subStr.substring(indexStart);
                    Set<String> cities = this.cityEpidemicMap.keySet();
                    for (String city : cities) {
                        String regEx = city + "?[0-9]+";
                        Pattern p1 = Pattern.compile(regEx);
                        Matcher m1 = p1.matcher(str);
                        if (m1.find()) {
                            // number示例：武汉市12345例
                            String number = m1.group();
                            Pattern p2 = Pattern.compile("[0-9]+");
                            Matcher m2 = p2.matcher(number);
                            if (m2.find()) {
                                Epidemic epidemic = cityEpidemicMap.get(city);
                                epidemic.setAccumulatedCuredCase(Integer.parseInt(m2.group()));
                            }
                        }
                    }
                }
            }

            //统计累计死亡
            if (sentence.contains("累计病亡")) {
                int index = sentence.indexOf("累计病亡");
                String subStr = sentence.substring(index);

                //全省病例
                Pattern p = Pattern.compile("[0-9]+");
                Matcher m = p.matcher(subStr);
                if (m.find()) {
                    totalEpidemic.setAccumulatedDeath(Integer.parseInt(m.group()));
                }

                //地级行政区病例
                int indexStart = subStr.indexOf("其中");
                if (indexStart >= 0) {
                    String str = subStr.substring(indexStart);
                    Set<String> cities = this.cityEpidemicMap.keySet();
                    for (String city : cities) {
                        String regEx = city + "?[0-9]+";
                        Pattern p1 = Pattern.compile(regEx);
                        Matcher m1 = p1.matcher(str);
                        if (m1.find()) {
                            // number示例：武汉市12345例
                            String number = m1.group();
                            Pattern p2 = Pattern.compile("[0-9]+");
                            Matcher m2 = p2.matcher(number);
                            if (m2.find()) {
                                Epidemic epidemic = cityEpidemicMap.get(city);
                                epidemic.setAccumulatedDeath(Integer.parseInt(m2.group()));
                            }
                        }
                    }
                }
            }

            //统计累计死亡
            if (sentence.contains("累计死亡")) {
                int index = sentence.indexOf("累计死亡");
                String subStr = sentence.substring(index);

                //全省病例
                Pattern p = Pattern.compile("[0-9]+");
                Matcher m = p.matcher(subStr);
                if (m.find()) {
                    totalEpidemic.setAccumulatedDeath(Integer.parseInt(m.group()));
                }

                //地级行政区病例
                int indexStart = subStr.indexOf("其中");
                if (indexStart >= 0) {
                    String str = subStr.substring(indexStart);
                    Set<String> cities = this.cityEpidemicMap.keySet();
                    for (String city : cities) {
                        String regEx = city + "?[0-9]+";
                        Pattern p1 = Pattern.compile(regEx);
                        Matcher m1 = p1.matcher(str);
                        if (m1.find()) {
                            // number示例：武汉市12345例
                            String number = m1.group();
                            Pattern p2 = Pattern.compile("[0-9]+");
                            Matcher m2 = p2.matcher(number);
                            if (m2.find()) {
                                Epidemic epidemic = cityEpidemicMap.get(city);
                                epidemic.setAccumulatedDeath(Integer.parseInt(m2.group()));
                            }
                        }
                    }
                }
            }

            //统计疑似病例
            if (sentence.contains("疑似病例")) {
                int index = sentence.indexOf("疑似病例");
                if (index >= 0) {
                    String subStr = sentence.substring(index);

                    //全省病例
                    Pattern p = Pattern.compile("[0-9]+");
                    Matcher m = p.matcher(subStr);
                    if (m.find()) {
                        totalEpidemic.setSuspectedCase(Integer.parseInt(m.group()));
                    }
                }
            }

            if (sentence.contains("密切接触者")) {
                int index = sentence.indexOf("密切接触者");
                if (index >= 0) {
                    String subStr = sentence.substring(index);

                    //全省病例
                    Pattern p = Pattern.compile("[0-9]+");
                    Matcher m = p.matcher(subStr);
                    if (m.find()) {
                        totalEpidemic.setCloseContacts(Integer.parseInt(m.group()));
                    }
                }
            }

            if (sentence.contains("医学观察")) {
                int index = sentence.indexOf("医学观察");
                if (index >= 0) {
                    String subStr = sentence.substring(index);

                    //全省病例
                    Pattern p = Pattern.compile("[0-9]+");
                    Matcher m = p.matcher(subStr);
                    if (m.find()) {
                        totalEpidemic.setMedicalObserver(Integer.parseInt(m.group()));
                    }
                }
            }
        }
    }
}
